package com.fisherman.condition;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import sun.plugin2.os.windows.Windows;

import java.awt.*;

/**
 * @User: fisherman
 * @Date: 2020/5/19 10:13
 */

//判断是否是Linux系统
public class DuanCondition implements Condition {
    /**
     * ConditionContext      判断条件能使用的上下午（环境）
     * AnnotatedTypeMetadata 注释信息
     *
     * @return
     */
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //判断是否是Linux系统
        //1.能获取到ioc使用的beanfactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //2.获取到类的加载器
        ClassLoader classLoader = context.getClassLoader();
        //3.获取当前环境信息
        Environment environment = context.getEnvironment();
        //4.获取到Bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();

        //判断操作系统
        String property = environment.getProperty("os.name");
        //判断下容器中是否包含person
       // BeanDefinition person = registry.getBeanDefinition("person");
        //可以判断容器中的bean注册情况，也可以给容器注册bean
        boolean definition = registry.containsBeanDefinition("person");
        if (property.contains("Linux")) {
            return true;
        }
        return false;
    }
}
